package com.ruge._05_springboot_web.config.fastjson;

import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
import org.springframework.boot.autoconfigure.http.HttpMessageConverters;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;

/**
 * springboot2 集成fastjson消息转换器
 *  <dependency>
 *      <groupId>com.alibaba</groupId>
 *      <artifactId>fastjson</artifactId>
 *      <version>1.2.56</version>
 * </dependency>
 */
@Configuration
public class FastJsonConfig {
    @Bean
    public HttpMessageConverters httpMessageConverter() {
        // 创建fastjson消息转换器
        FastJsonHttpMessageConverter fastJsonHttpMessageConverter = new FastJsonHttpMessageConverter();
        // 创建fastjson配置对象
        com.alibaba.fastjson.support.config.FastJsonConfig config = new com.alibaba.fastjson.support.config.FastJsonConfig();
        config.setSerializerFeatures(SerializerFeature.PrettyFormat);
        // 在Converter中添加配置
        fastJsonHttpMessageConverter.setFastJsonConfig(config);
        return new HttpMessageConverters((HttpMessageConverter) fastJsonHttpMessageConverter);

    }
}
